/**
 * 
 */
package au.edu.swin.videoshop.ejb.bean;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import au.edu.swin.videoshop.bean.Staff;
import au.edu.swin.videoshop.bean.StaffRole;
import au.edu.swin.videoshop.persistence.StaffEntity;
import au.edu.swin.videoshop.persistence.StaffSessionEntity;
import au.edu.swin.videoshop.service.StaffService;

/**
 * @author julie
 *
 */
@Stateless
public class StaffServiceBean implements StaffService {

	@PersistenceContext
	private EntityManager em;

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#addStaff(au.edu.swin.videoshop.bean.Staff)
	 */
	public void addStaff(Staff staff) {
		TransferService service = new TransferService();
		StaffEntity staffEntity = service.toStaffEntity(staff);
		em.persist(staffEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#updateStaff(au.edu.swin.videoshop.bean.Staff)
	 */
	public void updateStaff(Staff staff) {
		TransferService service = new TransferService();
		StaffEntity staffEntity = service.toStaffEntity(staff);
		em.merge(staffEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#markDeleteStaff(long)
	 */
	public void markDeleteStaff(long id) {
		StaffEntity staffEntity = em.find(StaffEntity.class, id);
		if (staffEntity != null) {
			staffEntity.setDeleted(true);
			em.merge(staffEntity);
		}
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#login(java.lang.String, java.lang.String)
	 */
	public Staff login(String userName, String password) {
		Query query = em.createNamedQuery("authenticateStaff");
		query.setParameter("userName", userName);
		query.setParameter("password", password);
		
		try {
			StaffEntity staffEntity = (StaffEntity)query.getSingleResult();
					
			if (staffEntity != null) {
				StaffSessionEntity staffSessionEntity = new StaffSessionEntity();
				staffSessionEntity.setStaff(staffEntity);
				staffSessionEntity.setLoginTime(new Date());
				
				em.persist(staffSessionEntity);
				
				TransferService service = new TransferService();
				
				Staff staff = service.toStaffTransferObject(staffEntity);
				staff.setCurrentSession(service.toStaffSessionTransferObject(staffSessionEntity));
				
				return staff;
			}
		}
		catch (NoResultException ex) {
			
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#logout(au.edu.swin.videoshop.bean.Staff)
	 */
	public void logout(Staff staff) {
		TransferService service = new TransferService();
		StaffSessionEntity staffSessionEntity = service.toStaffSessionEntity(staff.getCurrentSession());
		staffSessionEntity.setLogoutTime(new Date());
		
		em.merge(staffSessionEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#changePassword(java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean changePassword(String userName, String currentPassword, String password) {
		Query query = em.createNamedQuery("changeStaffPassword");
		query.setParameter("userName", userName);
		query.setParameter("currentPassword", currentPassword);
		query.setParameter("password", password);
		
		return query.executeUpdate() == 1;
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#changeStaffRole(long, au.edu.swin.videoshop.bean.UserRole)
	 */
	public void changeStaffRole(long id, StaffRole role) {
		Query query = em.createNamedQuery("changeStaffRole");
		query.setParameter("staffId", id);
		query.setParameter("staffRole", role.getName());
		
		query.executeUpdate();
	}

	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#findStaffByStaffId(long)
	 */
	public Staff findStaffByStaffId(long id) {
		StaffEntity staffEntity = em.find(StaffEntity.class, id);
		if (staffEntity != null) {
			TransferService service = new TransferService();
			return service.toStaffTransferObject(staffEntity);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#findAllStaffs()
	 */
	public List<Staff> findAllStaffs() {
		Query query = em.createNamedQuery("findAllStaffs");
		List list = query.getResultList();
		TransferService service = new TransferService();
		return service.toStaffTransferObjectList(list);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StaffService#findStaffsByStoreId(long)
	 */
	public List<Staff> findStaffsByStoreId(long storeId) {
		Query query = em.createNamedQuery("findStaffsByStoreId");
		query.setParameter("storeId", storeId);
		List list = query.getResultList();
		TransferService service = new TransferService();
		return service.toStaffTransferObjectList(list);
	}
}
